const gulp = require("gulp")
const server = require("gulp-webserver")
const fs = require("fs")
const path = require("path")
const url = require("url")

gulp.task("server",() => {
    return gulp.src("./src")
    .pipe(server({
        open:true,
        middleware:(req,res) => {
            let pathname = url.parse(req.url).pathname;
            if(pathname == "/favicon.ico"){
                return false
            }
            if(pathname == "/list"){
                res.end(JSON.stringify({code:0,asg:"list"}))
            }else if(pathname == "/uid"){
                let uid = url.parse(req.url,true).query
                res.end(JSON.stringify({code:0,asg:uid}))
            }else{
                pathname = pathname == "/" ? "index.html" : pathname
                res.end(fs.readFileSync(path.join(__dirname,"src",pathname)))
            }
            
        }
    }))
})